//===============================================================================
// Copyright (C) 2008 Charles River Media. All rights reserved.
// 
// Catmull-Clark subdivision surface viewer program
//
// The developer of the code is
// Gyorgy Antal <gyantal@gmail.com>
//
// Contributor(s):
// Laszlo Szirmay-Kalos <szirmay@iit.bme.hu>
//
// The software was developed for the chapter 
// "Fast Evaluation of Subdivision Surfaces on DirectX10 Graphics Hardware"
// of the book ShaderX 6: Advanced Rendering Techniques, 
// edited by Wolfgang Engel,
// the appropriate section edited by Tom Forsyth. 
//
// The files are provided without support, instruction, or implied warranty of any
// kind.  Charles River Media makes no guarantee of its fitness for a particular purpose and is
// not liable under any circumstances for any damages or loss whatsoever arising
// from the use or inability to use this file or items derived from it.
//===============================================================================
// File: File.cpp
//
// Description:
//===============================================================================
	
#include <process.h>
#include "file.h"
#include <share.h>     /* for _SH_DENYNO */

//===================================================================
//                      class File
//===================================================================

//-------------------------------------------------------------------        
File :: File(char * n) {
//-------------------------------------------------------------------
	buffer = new char[BUFFERSIZE]; 
 	ptr = 0; name = n;
}



//===================================================================
//                      class InputFile
//===================================================================


//-------------------------------------------------------------------
InputFile :: InputFile(char * n) : File(n)
//-------------------------------------------------------------------
{
//	fhandle = _sopen_s(n, O_RDONLY, 0666);

	// Open a file
   _sopen_s(&fhandle, n, _O_RDONLY, _SH_DENYNO, _S_IREAD);
  
	if (fhandle < 0){
		//cerr << "\nFile " << n << " cannot be opened!\n";
		exit(-1);
	} else Init();
}

//-------------------------------------------------------------------
char InputFile :: InspectNext()
//-------------------------------------------------------------------
{
    if (ptr >= nchar) {
		ptr = 0;
        nchar = _read(fhandle, buffer, BUFFERSIZE);
        if (nchar <= 0) {
			eof = TRUE;
            return EOF;
        }
            
  }
    return buffer[ ptr ];
}

//-------------------------------------------------------------------
char InputFile :: Read() {
//-------------------------------------------------------------------
 	if (eof) return EOF;
        char c = InspectNext();
        ptr++;
        file_ptr++;
        return c;
}

//-------------------------------------------------------------------
void InputFile :: Reset() {
//-------------------------------------------------------------------
	_lseek(fhandle, (off_t)0, SEEK_SET);	
	
	file_ptr = 0;
	ptr = 0;
	nchar = 0;
	eof = FALSE;
}

